package com.ljw.food_order_server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ljw.food_order_server.entity.Food;
import com.ljw.food_order_server.entity.MyWindow;
import com.ljw.food_order_server.mapper.FoodMapper;
import com.ljw.food_order_server.service.FoodService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单 服务实现类
 * </p>
 *
 * @author ljw
 * @since 2021-07-31
 */
@Service
public class FoodServiceImpl extends ServiceImpl<FoodMapper, Food> implements FoodService {

    @Autowired
    private MyWindowServiceImpl myWindowService;

    @Autowired
    private MyWindowServiceImpl windowService;

    public List<Food> getFoodsByWindowId(Integer windowId){
        QueryWrapper<Food> foodQueryWrapper = new QueryWrapper<>();
        foodQueryWrapper.eq("window_id",windowId);
        List<Food> list = this.list(foodQueryWrapper);
        return list;
    }

    public Food getFoodRandom(Integer positionId){
        List<MyWindow> windows = myWindowService.getWindows(positionId);
        List<Integer> windowIds = new ArrayList<>();
        windows.forEach(w -> windowIds.add(w.getWindowId()));
        QueryWrapper<Food> foodQueryWrapper = new QueryWrapper<>();
        foodQueryWrapper.in("window_id", windowIds);
        List<Food> list = this.list(foodQueryWrapper);
        int random = getRandomInt(list.size());
        return list.get(random);
    }

    public List<Food> getFoodsBlur(int positionId, String word){
        List<MyWindow> windows = windowService.getWindows(positionId);
        List<Integer> windowIds = new ArrayList<>();
        windows.forEach(w -> windowIds.add(w.getWindowId()));
        QueryWrapper<Food> foodQueryWrapper = new QueryWrapper<>();
        foodQueryWrapper.like("food_name",word).in("window_id",windowIds);
        return this.list(foodQueryWrapper);
    }

    private int getRandomInt(int max) {
        return (int) Math.floor(Math.random() * max);
    }


}
